//
//  WXTabBarController.m
//  MiniWeChat
//
//  Created by ByteDance on 2025/6/24.
//

#import "WXTabBarController.h"
#import "WXNavigationController.h"
#import "HomeViewController.h"
#import "UserModel.h"
#import "DBStorageService.h"
#import "ConversationListViewModel.h"
#import "StrUtil.h"
#import "MineViewController.h"

@interface WXTabBarController ()

@end

@implementation WXTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViewControllers];
}

- (void)setupViewControllers {
    
//    NSArray *tabBars = @[
//        @{
//            @"VCClassString": @"HomViewController",
//            @"title": @"首页",
//            @"image": @"home",
//            @"selectedImage": @"home_h"
//        },
//        @{
//            @"VCClassString": @"MineViewController",
//            @"title": @"我的",
//            @"image": @"mine",
//            @"selectedImage": @"mine_h"
//        },
//    ];
//    
//    [tabBars enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx,  BOOL *stop) {
//        UIViewController *vc = [NSClassFromString(dict[@"VCClassString"]) new];
//        vc.title = dict[@"title"];
//        WXNavigationController *nav = [[WXNavigationController alloc] initWithRootViewController:vc];
//        UITabBarItem *item = nav.tabBarItem;
//        item.title = dict[@"title"];
//        item.image = [UIImage imageNamed:dict[@"image"]];
//        item.selectedImage = [[UIImage imageNamed:dict[@"selectedImage"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//        [item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0 green:(190 / 255.0) blue:(12 / 255.0) alpha:1]} forState:UIControlStateSelected];
//        [self addChildViewController:nav];
//    }];
    
//    [self initializeData];
    // 1. 创建首页视图控制器
    HomeViewController *homeVC = [[HomeViewController alloc] initWithViewModel:[[ConversationListViewModel alloc] initWithUID:@"1001"]];
    homeVC.title = @"微信";
    // 2. 创建导航视图控制器
    WXNavigationController *homeNav = [[WXNavigationController alloc] initWithRootViewController:homeVC];
    // 3. 设置标签页属性
    homeNav.tabBarItem.title = @"首页";
    homeNav.tabBarItem.image = [UIImage imageNamed:@"home"];
    homeNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"home_h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [homeNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0 green:(190 / 255.0) blue:(12 / 255.0) alpha:1]} forState:UIControlStateSelected];
    
    
    // 1. 创建我的视图控制器
    MineViewController *mineVC = [[MineViewController alloc] init];
    // 2. 创建导航视图控制器
    WXNavigationController *mineNav = [[WXNavigationController alloc] initWithRootViewController:mineVC];
    // 3. 设置标签页属性
    mineNav.tabBarItem.title = @"我的";
    mineNav.tabBarItem.image = [UIImage imageNamed:@"mine"];
    mineNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"mine_h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [mineNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0 green:(190 / 255.0) blue:(12 / 255.0) alpha:1]} forState:UIControlStateSelected];
    
    // 设置标签文字样式
    NSDictionary *titleTextAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:12.0],
    };
    [[UITabBarItem appearance] setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
    
    // 设置样式
    UITabBar *tabBar = [UITabBar appearance];
    [tabBar setTintColor:[UIColor colorWithRed:0 green:(190 / 255.0) blue:(12 / 255.0) alpha:1]];
    
    self.viewControllers = @[homeNav, mineNav];
}

- (void)initializeData {
    DBStorageService *storage = [DBStorageService sharedInstance];
    NSArray<UserModel *> *users = [self generateUsers];
    for (UserModel *user in users) {
        // batch save
        [storage saveUser:user];
    }
    NSArray<ConversationModel *> *conversations = [self generateConversations];
    for (ConversationModel *conversation in conversations) {
        [storage saveConversation:conversation];
    }
    NSArray<MessageModel *> *messages = [self generateMessagesWithConversationId:[conversations objectAtIndex:0].id];
    for (MessageModel *message in messages) {
        [storage saveMessage:message];
    }
}

- (NSArray<UserModel *> *)generateUsers{
    NSMutableArray<UserModel *> *users = [NSMutableArray array];
    
    UserModel *sender = [[UserModel alloc] init];
    sender.id = @"1001";
    sender.nickname = @"发送者";
    sender.avatar = @"avatar";
    sender.signature = @"发送者个性签名";
    [users addObject:sender];
    
    UserModel *receiver = [[UserModel alloc] init];
    receiver.id = @"1002";
    receiver.nickname = @"接收者";
    receiver.avatar = @"WechatIMG5";
    receiver.signature = @"接收者个性签名";
    [users addObject:receiver];
    
    return users;
}

- (NSArray<ConversationModel *> *)generateConversations {
    NSMutableArray<ConversationModel *> *conversations = [NSMutableArray array];
    ConversationModel *conversation = [[ConversationModel alloc] init];
    NSArray<UserModel *> *users = [self generateUsers];
    UserModel *sender = [users objectAtIndex:0];
    UserModel *receiver = [users objectAtIndex:1];
    conversation.id = [StrUtil concatenateString:receiver.id withString:sender.id];
    conversation.type = 1;
    conversation.nickname = receiver.nickname;
    conversation.avatar = receiver.avatar;
    [conversations addObject:conversation];
    return conversations;
}

- (NSArray<MessageModel *> *)generateMessagesWithConversationId:(NSString *)conversation_id {
    NSMutableArray<MessageModel *> *messages = [NSMutableArray array];
    MessageModel *message = [[MessageModel alloc] init];
    message.id = [[NSUUID UUID] UUIDString];
    message.conversation_id = conversation_id;
    NSArray<NSString *> *strs = [StrUtil splitConcatenatedString:conversation_id];
    message.sender = [strs objectAtIndex:1];
    message.receiver = [strs objectAtIndex:0];
    message.content = [NSString stringWithFormat:@"你好！"];
    message.time = [[NSDate date]timeIntervalSince1970];
    
    return messages;
}

@end
